package com.demo.juc.volat;

/**
 * @Author: yaoshubin
 * @Date: 2023-07-23  10:28
 * @Description: Volatile 禁止指令重排
 * 不影响：int a = 1；int b = 2;  --> int b = 2;int a = 1
 * 影响： a = 1; b = a; --> b = a; a = 1; 显然不一样
 *
 * 本节：volatile 读写顺序通过内存屏障保证 指令执行顺序
 */
public class VolatileCommdDemo {

    int i = 0;
    /*volatile*/ boolean flag = true;

    public void write() {
        i = 2;
        flag = false;
    }

    public void read() {
        if (flag) {
            System.out.println("当前i:" + i);
        }
    }
}
